package com.jili.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jili.annotion.UserLog;
import com.jili.entity.User;
import com.jili.mapper.UserMapper;
import com.jili.rsp.CommonRsp;
import com.jili.rsp.Result;
import com.jili.service.UserService;
import com.jili.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: UserController
 * Package com.jili.controller
 * Description:
 *
 * @Author: 廖继礼
 * @Create: 2025/2/23
 * @Version: v1.0
 */
@RequestMapping("/user")
@RestController
public class UserController {

    public static final int size = 2;

    public static final int one = 1;

    public static final int two = 2;

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @GetMapping("/query/{id}")
    public Result selectUserById(@PathVariable Integer id) {
        return userService.selectUserById(id);
    }

    @GetMapping("/page")
    public Result queryUser() {
        return userService.selectPage(1, 2);
    }

    @GetMapping("/while")
    public Result queryByWhile() {
        return userService.selectWhile();
    }

    @GetMapping("/json")
    public Result queryUserToJson() {
        List<User> userList = userMapper.selectList(null);
        List<UserVo> list = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        userList.forEach(user -> {
            try {
                UserVo userVo = new UserVo();
                userVo.setObjId(user.getId());
                userVo.setRecords(objectMapper.writeValueAsString(user));
                list.add(userVo);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
        return new Result().ok(list);
    }

    @GetMapping("/queryPage")
    public CommonRsp queryPage(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                               @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return userService.queryPage(currentPage, pageSize);
    }
}
